package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.List;
import java.util.Scanner;


@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择操作：1.注册 2.登录 3.展示用户列表 4.修改用户信息");
        int choice = sc.nextInt();
        switch (choice){
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdata();
                break;
        }
//        userMapper.insert(null);
//        System.out.println("UserController:用户注册成功");
    }

    private void userReg(){
        System.out.println("欢迎注册");
        Scanner sc = new Scanner(System.in);
        User user = new User();
        System.out.println("请输入用户名：");
        user.setUsername(sc.nextLine());
        System.out.println("请输入密码：");
        user.setPassword(sc.nextLine());
        System.out.println("请输入昵称：");
        user.setNickname(sc.nextLine());
        System.out.println("请输入年龄：");
        user.setAge(sc.nextInt());
        System.out.println(user);
        if (userMapper.selectByUsername(user.getUsername()) == null){
            int result = userMapper.insert(user);
            System.out. println(result > 0 ? "用户注册成功" : "用户注册失败");
        }else {
            System.out.println("用户【"+user.getUsername()+"】已存在");
        }
    }

    private void userLogin(){
        System.out.println("欢迎登录");
        Scanner sc = new Scanner(System.in);
        User user = new User();
        System.out.println("请输入用户名：");
        user.setUsername(sc.nextLine());
        System.out.println("请输入密码：");
        user.setPassword(sc.nextLine());
        System.out.println(user);
        int result = userMapper.select(user);
        System.out.println(result > 0 ? "用户登录成功" : "用户登录失败");

    }

//    private void userSelectByUsername(){
//        Scanner sc = new Scanner(System.in);
//        System.out.println("请输入用户名：");
//        String username = sc.nextLine();
//        System.out.println("请输入密码：");
//        String password = sc.nextLine();
//        User user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
//        User user1 = userMapper.selectByUsername(user);
//    }
    private void userList(){
        System.out.println("展示用户列表");
        List<User> users = userMapper.selectAll();
        for (User user : users){
            System.out.println( user);
        }
    }
    // 修改用户信息
    private void userUpdata(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要更改的用户名：");
        String username = sc.nextLine();
        System.out.println("请输入新密码：");
        String password = sc.nextLine();
        System.out.println("请输入新昵称：");
        String nickname = sc.nextLine();
        System.out.println("请输入新年龄：");
        int age = sc.nextInt();
        User user = new User(username,password,nickname,age);
        int result = userMapper.userUpdate(user);
        System.out.println(result > 0 ? "用户信息修改成功" : "用户信息修改失败");
    }
}
